# @Version        : 1.0
# @Update Time    : 2025/8/17 15:27
# @File           : admin_user.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息
from datetime import datetime
from typing import Optional

from fastapi_utils.api_model import APIModel
from pydantic import BaseModel, Field, field_validator

from applications.schemas.common import BaseQueryParams


class UserQueryParams(BaseQueryParams):
    username: Optional[str] = Field(None, description="用户名")
    deptId: Optional[int] = Field(None, description="部门ID")
    realname: Optional[str] = Field(None, description="真实姓名")


class UserAddSchema(APIModel):
    username: str = Field(..., min_length=4, max_length=20, description="用户名")
    password: str = Field(..., min_length=4, max_length=20, description="密码")
    realname: str = Field(..., min_length=2, max_length=20, description="真实姓名")
    dept_id: int = Field(..., description="部门ID")
    roleIds: str = Field(..., description="角色ID")
    enable: int = 1


class UserUpdateSchema(APIModel):
    userId: int = Field(..., description="用户ID")
    username: str = Field(..., min_length=4, max_length=20, description="用户名")
    realname: str = Field(..., min_length=2, max_length=20, description="真实姓名")
    deptId: int = Field(..., description="部门ID")
    roleIds: str = Field(..., description="角色ID")


class UserOut(APIModel):
    id: int = Field(..., description="用户ID")
    username: str = Field(..., description="用户名")
    realname: str = Field(..., description="真实姓名")
    dept_id: Optional[int] = Field(description="部门ID")
    dept_name: Optional[str] = Field(description="部门ID")
    enable: int = Field(..., description="状态(1开启,0关闭)")
    create_at: datetime = Field(..., description="创建时间")
    update_at: datetime = Field(..., description="更新时间")


class UpdateAvatarSchema(BaseModel):
    avatar: dict = Field(..., description="头像信息")


class UpdateInfoSchema(BaseModel):
    realName: str = Field(..., description="真实姓名")
    details: Optional[str] = Field(None, description="详细信息")


class UpdatePasswordSchema(BaseModel):
    oldPassword: str = Field(..., description="旧密码")
    newPassword: str = Field(..., description="新密码")
    confirmPassword: str = Field(..., description="确认密码")

    @field_validator("newPassword")
    @classmethod
    def password_must_not_be_empty(cls, v):
        if v == "":
            raise ValueError("新密码不得为空")
        return v

    @field_validator("confirmPassword")
    @classmethod
    def passwords_must_match(cls, v, info):
        if "newPassword" in info.data and v != info.data["newPassword"]:
            raise ValueError("两次密码不一致")
        return v


class EnableDisableUserSchema(BaseModel):
    userId: int = Field(..., description="用户ID")
